package com.ipemon.model;

public class IPemon {

	private int identifiant;
	private int espece;
	private int vieCur;
	private int vieMax;
	private int experience;
	private int rapidite;
	private Power attaque;
	private Power defense;
	
	public int getIdentifiant() {
		return identifiant;
	}
	public void setIdentifiant(int identifiant) {
		this.identifiant = identifiant;
	}
	
	public int getEspece() {
		return espece;
	}
	
	public String getName() {
		return "IPemon" + this.identifiant;
	}
	public String getEspeceName() {
		return IPemonDirectory.getEspeceName(this.espece);
	}
	
	public String getIdentity() {
		return getName() + " [" + getEspeceName() + "]";
	}
	
	public void setEspece(int espece) {
		this.espece = espece;
	}
	public int getVieCur() {
		return vieCur;
	}
	public void setVieCur(int vieCur) {
		this.vieCur = vieCur;
	}
	public int getVieMax() {
		return vieMax;
	}
	public void setVieMax(int vieMax) {
		this.vieMax = vieMax;
	}
	public int getExperience() {
		return experience;
	}
	public void setExperience(int experience) {
		this.experience = experience;
	}
	public int getRapidite() {
		return rapidite;
	}
	public void setRapidite(int rapidite) {
		this.rapidite = rapidite;
	}
	public Power getAttaque() {
		return attaque;
	}
	public void setAttaque(Power attaque) {
		this.attaque = attaque;
	}
	public Power getDefense() {
		return defense;
	}
	public void setDefense(Power defense) {
		this.defense = defense;
	}
	
	private static int[] getNumberSeparatedByColon(String data) {
		
		String numberString[] = data.split(":");
		int numbers[] = new int[numberString.length];
		
		for(int i=0; i<numberString.length; i++) {
			numbers[i] = Integer.valueOf(numberString[i]);
		}
		
		return numbers;
	}
	
	private static Power getCompetenceByNumber(int[] competenceNumber) {
		Power newCompetence = new Power();
		newCompetence.setFeu(competenceNumber[0]);
		newCompetence.setEau(competenceNumber[1]);
		newCompetence.setElectric(competenceNumber[2]);
		newCompetence.setPlante(competenceNumber[3]);
		newCompetence.setAir(competenceNumber[4]);
		newCompetence.setPierre(competenceNumber[5]);
		
		return newCompetence;
	}
	
	public static IPemon getIPemonByDataStream(String dataStream) {
		
		String characteristics = dataStream.substring(0, dataStream.indexOf("("));
		dataStream = dataStream.substring(dataStream.indexOf("(")+1);
		String attaque = dataStream.substring(0, dataStream.indexOf(")"));
		dataStream = dataStream.substring(dataStream.indexOf("(")+1);
		String defense = dataStream.substring(0, dataStream.indexOf(")"));
		
		int characterData[] = IPemon.getNumberSeparatedByColon(characteristics);
		int attaqueData[] = IPemon.getNumberSeparatedByColon(attaque);
		int defenseData[] = IPemon.getNumberSeparatedByColon(defense);

		IPemon newIPemon = new IPemon();
		
		newIPemon.setIdentifiant(characterData[0]);
		newIPemon.setEspece(characterData[1]);
		newIPemon.setVieCur(characterData[2]);
		newIPemon.setVieMax(characterData[3]);
		newIPemon.setExperience(characterData[4]);
		newIPemon.setRapidite(characterData[5]);
		
		newIPemon.setAttaque(IPemon.getCompetenceByNumber(attaqueData));
		newIPemon.setDefense(IPemon.getCompetenceByNumber(defenseData));
		
		return newIPemon;
	}
	
	public String getIPemonStream(){
		String stream = "";
		
		stream += String.valueOf(this.getIdentifiant()) + ":";
		stream += String.valueOf(this.getEspece()) + ":";
		stream += String.valueOf(this.getVieCur()) + ":";
		stream += String.valueOf(this.getVieMax()) + ":";
		stream += String.valueOf(this.getExperience()) + ":";
		stream += String.valueOf(this.getRapidite()) + ":(";
		
		stream += String.valueOf(this.getAttaque().getFeu()) + ":";
		stream += String.valueOf(this.getAttaque().getEau()) + ":";		
		stream += String.valueOf(this.getAttaque().getElectric()) + ":";		
		stream += String.valueOf(this.getAttaque().getPlante()) + ":";		
		stream += String.valueOf(this.getAttaque().getAir()) + ":";		
		stream += String.valueOf(this.getAttaque().getPierre()) + "):(";		
		
		stream += String.valueOf(this.getDefense().getFeu()) + ":";	
		stream += String.valueOf(this.getDefense().getEau()) + ":";	
		stream += String.valueOf(this.getDefense().getElectric()) + ":";	
		stream += String.valueOf(this.getDefense().getPlante()) + ":";	
		stream += String.valueOf(this.getDefense().getAir()) + ":";	
		stream += String.valueOf(this.getDefense().getPierre()) + ")";	
		
		return stream;
	}
	
	public String toString() {
		
		String returnString = "";
		
		returnString += "Identifiant:\t\t" + this.getName() + " [" + getEspeceName() + "]" + "\n";
		returnString += "Vie: \t\t" + this.getVieCur() + "/" + this.getVieMax() + "\n";
		returnString += "Rapidity: \t" + this.getRapidite() + "\n";
		returnString += "Experience: \t" + this.getExperience() + "\n";
		returnString += "Competence\n";

		returnString += "\t\t" + "Attaque" + "\tDefense\n";
		returnString += "Feu\t\t" + " " + getAttaque().getFeu() + "\t\t" + getDefense().getFeu() + "\n"; 
		returnString += "Eau\t\t" + " " + getAttaque().getEau() + "\t\t" + getDefense().getEau() + "\n"; 
		returnString += "Elec\t\t" + getAttaque().getElectric() + "\t\t" + getDefense().getElectric() + "\n"; 
		returnString += "Plante\t" + "  " + getAttaque().getPlante() + "\t\t" + getDefense().getPlante() + "\n"; 
		returnString += "Air\t\t" + " " + getAttaque().getAir() + "\t\t" + getDefense().getAir() + "\n"; 
		returnString += "Pierre\t" + "  " + getAttaque().getPierre() + "\t\t" + getDefense().getPierre() + "\n"; 
		
		return returnString;
	}
	
	public static void main(String[] args) {
		IPemon.getIPemonByDataStream("3:56:70:98:8:0:(4:0:4:3:3:6):(4:4:3:4:6:4)");
	}
	
}
